﻿'Imports System.Runtime.InteropServices

'Public Class Form1

'    Private tb As New TextBox
'    Private WithEvents but1 As New Button

'    Sub New()
'        InitializeComponent()
'        tb = New TextBox : tb.Location = New Point(10, 10)
'        but1 = New Button : but1.Location = New Point(10, 40)
'        but1.Text = "example"
'        Me.Controls.Add(but1) : Me.Controls.Add(tb)
'    End Sub

'    Private Sub DoMouse(ByVal flags As NativeMethods.MOUSEEVENTF, ByVal newPoint As Point)
'        Dim input As New NativeMethods.INPUT
'        Dim mi As New NativeMethods.MOUSEINPUT
'        input.dwType = NativeMethods.InputType.Mouse
'        input.mi = mi
'        input.mi.dwExtraInfo = IntPtr.Zero
'        ' mouse co-ords: top left is (0,0), bottom right is (65535, 65535)
'        ' convert screen co-ord to mouse co-ords...
'        input.mi.dx = newPoint.X * (65535 / Screen.PrimaryScreen.Bounds.Width)
'        input.mi.dy = newPoint.Y * (65535 / Screen.PrimaryScreen.Bounds.Height)
'        input.mi.time = 0
'        input.mi.mouseData = 0  ' can be used for WHEEL event see msdn
'        input.mi.dwFlags = flags
'        Dim cbSize As Integer = Marshal.SizeOf(GetType(NativeMethods.INPUT))
'        Dim result As Integer = NativeMethods.SendInput(1, input, cbSize)
'        If result = 0 Then Debug.WriteLine(Marshal.GetLastWin32Error)
'    End Sub

'    Private Sub DoKeyBoard(ByVal flags As NativeMethods.KEYEVENTF, ByVal key As Keys)
'        Dim input As New NativeMethods.INPUT
'        Dim ki As New NativeMethods.KEYBDINPUT
'        input.dwType = NativeMethods.InputType.Keyboard
'        input.ki = ki
'        input.ki.wVk = Convert.ToInt16(key)
'        input.ki.wScan = 0
'        input.ki.time = 0
'        input.ki.dwFlags = flags
'        input.ki.dwExtraInfo = IntPtr.Zero
'        Dim cbSize As Integer = Marshal.SizeOf(GetType(NativeMethods.INPUT))
'        Dim result As Integer = NativeMethods.SendInput(1, input, cbSize)
'        If result = 0 Then Debug.WriteLine(Marshal.GetLastWin32Error)
'    End Sub

'    Private Sub but1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles but1.Click
'        'move to the textbox, click it to focus, keydown some chars...
'        ' get textbox location in screen co-ords
'        Dim tbLocation As Point = Me.PointToScreen(Me.tb.Location)
'        ' nudge it into the tb
'        tbLocation.X += 5
'        tbLocation.Y += 5
'        ' move to the TB. it is a MOVE event, and we use ABSOLUTE co-ordinates
'        DoMouse(NativeMethods.MOUSEEVENTF.MOVE Or NativeMethods.MOUSEEVENTF.ABSOLUTE, tbLocation)
'        ' click the TB
'        DoMouse(NativeMethods.MOUSEEVENTF.LEFTDOWN, New Point(0, 0))
'        ' release the mouse
'        DoMouse(NativeMethods.MOUSEEVENTF.LEFTUP, New Point(0, 0))
'        ' the key codes are virtual keycodes (see mdsn)
'        ' I'm using a shortcut to save space - casting chars to ints
'        ' if you want to change case, you need to send a shift key with the letter...
'        ' sbq: I found the routine VkKeyScanA very helpful converting from
'        '      characters to virtual key codes.  Look it up in the MSDN.  I declared it
'        '      as shown below.  It returns the key code in the low byte and the additional
'        '      keys to be pressed while transmitting in the upper byte (e.g., shift, ctrl, alt):
'        ' 
'        '      Private Declare Function VkKeyScanA Lib "user32" Alias "VkKeyScanA" (ByVal cChar_Renamed As Byte) As Short
'        '
'        Dim message As String = "HELLO WORLD"
'        For Each c As Char In message
'            Dim key As UInteger = Convert.ToInt16(c)
'            DoKeyBoard(0, key)  ' key down is 0 - no flag...
'            DoKeyBoard(NativeMethods.KEYEVENTF.KEYUP, key)
'        Next
'        DoKeyBoard(0, Keys.Decimal) ' example using keys enum which I think matches the VKEY values
'        DoKeyBoard(NativeMethods.KEYEVENTF.KEYUP, Keys.Decimal)
'    End Sub

'End Class

'Public Class NativeMethods

'    <DllImport("user32.dll", SetLastError:=True)> _
'    Friend Shared Function SendInput(ByVal cInputs As Int32, ByRef pInputs As INPUT, ByVal cbSize As Int32) As Int32
'    End Function

'    <StructLayout(LayoutKind.Explicit, pack:=1, Size:=28)> _
'    Friend Structure INPUT
'        <FieldOffset(0)> Public dwType As InputType
'        <FieldOffset(4)> Public mi As MOUSEINPUT
'        <FieldOffset(4)> Public ki As KEYBDINPUT
'        <FieldOffset(4)> Public hi As HARDWAREINPUT
'    End Structure

'    <StructLayout(LayoutKind.Sequential, pack:=1)> _
'    Friend Structure MOUSEINPUT
'        Public dx As Int32
'        Public dy As Int32
'        Public mouseData As Int32
'        Public dwFlags As MOUSEEVENTF
'        Public time As Int32
'        Public dwExtraInfo As IntPtr
'    End Structure

'    <StructLayout(LayoutKind.Sequential, pack:=1)> _
'    Friend Structure KEYBDINPUT
'        Public wVk As Int16
'        Public wScan As Int16
'        Public dwFlags As KEYEVENTF
'        Public time As Int32
'        Public dwExtraInfo As IntPtr
'    End Structure

'    <StructLayout(LayoutKind.Sequential, pack:=1)> _
'    Friend Structure HARDWAREINPUT
'        Public uMsg As Int32
'        Public wParamL As Int16
'        Public wParamH As Int16
'    End Structure

'    Friend Enum InputType As Integer
'        Mouse = 0
'        Keyboard = 1
'        Hardware = 2
'    End Enum

'    <Flags()> _
'    Friend Enum MOUSEEVENTF As Integer
'        MOVE = &H1
'        LEFTDOWN = &H2
'        LEFTUP = &H4
'        RIGHTDOWN = &H8
'        RIGHTUP = &H10
'        MIDDLEDOWN = &H20
'        MIDDLEUP = &H40
'        XDOWN = &H80
'        XUP = &H100
'        VIRTUALDESK = &H400
'        WHEEL = &H800
'        ABSOLUTE = &H8000
'    End Enum

'    <Flags()> _
'    Friend Enum KEYEVENTF As Integer
'        EXTENDEDKEY = 1
'        KEYUP = 2
'        [UNICODE] = 4
'        SCANCODE = 8
'    End Enum
'End Class

